document.querySelector('.img__btn').addEventListener('click', function() {
    document.querySelector('.content').classList.toggle('s--signup')
})

$("#reg_uname").blur(function (){
    let username = $("#reg_uname").val()
    if (!/^[a-zA-Z0-9_-]{4,16}$/.test(username)){
        $("#reg_uname_msg").text("用户名不符合标准")
        return ;
    }
    console.log(username);

    $.post("/yuezhi/login/uname.action",{"uname":username},function (date){
        console.log(date)
        if (date.code=="301"){
            console.log("301")
            $("#reg_uname_msg").text("")
        }
        if (date.code=="302"){
            console.log("302")
            $("#reg_uname_msg").text("用户名已存在")
        }
    })
})
$("#reg_phone").blur(function (){
    let phone = $("#reg_phone").val()
    if (!/^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/.test(phone)){
        $("#reg_phone_msg").text("手机号不符合标准")
        return ;
    }else{
        $("#reg_phone_msg").text("")
    }
})
var wait=60
function time(obj) {
    if (wait == 0) {
        obj.removeAttribute("disabled");
        obj.innerHTML = "获取验证码";
        wait = 60;
    } else {
        obj.setAttribute("disabled", true);
        obj.innerHTML = wait + "秒后重新发送";
        wait--;
        setTimeout(function () {
                time(obj)
            },
            1000)
    }
}
var code
function codecheck(obj){
    let ms  = $("#reg_phone_msg")
    let phone = $("#reg_phone").val()
    let msg = ms.text()
    console.log(msg)
    console.log(phone)
    if (phone!=""&&msg==""){
        console.log(1)
        $("#reg_code_msg").text("")
        time(obj)
        $.post("/yuezhi/login/sms.action",{"phone":phone},function (data) {
            code = data.data;
            console.log(data.data);
        })
    }else{
        console.log(2)
        $("#reg_code_msg").text("手机号码有误")
    }
}
$("#reg_code").blur(function () {
    let inputcode = $("#reg_code").val();
    if (inputcode==code){
        $("#reg_code_msg").text("")
    }else{
        $("#reg_code_msg").text("验证码有误")
    }
})

$("#reg_password").blur(function () {
    let password = $("#reg_password").val();
    console.log(password)
    if ( !/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{8,16}$/.test(password)){
        $("#reg_password_msg").text("至少8-16个字符，至少1个大写字母，1个小写字母和1个数字")
    }else{
        $("#reg_password_msg").text("")
    }
})
$("#reg").click(function () {
    let nickname_msg = $("#reg_uname_msg").text();
    let phone_msg = $("#reg_phone_msg").text()
    let code_msg = $("#reg_code_msg").text()
    let password_msg =$("#reg_password_msg").text()
    let nickname = $("#reg_uname").val();
    let phone = $("#reg_phone").val()
    let password = $("#reg_password").val();
    let code = $("#reg_code").val()

    console.log(nickname)
    console.log(phone)
    console.log(password)
    console.log(code)
    console.log(nickname_msg);
    console.log(phone_msg);
    console.log(code_msg);
    console.log(password_msg)


    if (nickname!=""&&phone!=""&&password!=""&&code!=""&&nickname_msg==""&&phone_msg==""&&code_msg==""&&password_msg==""){
        $.post("/yuezhi/login/register.action",{"usNickname":nickname,"usPhone":phone,"usPassword":password},function (data) {
            if (data.msg=="注册成功"){
                $("#reg_uname").val("");
                $("#reg_phone").val("")
                $("#reg_password").val("");
                $("#reg_code").val("")
                wait=0
                document.querySelector('.content').classList.toggle('s--signup')
            }
            console.log(data)
        })
    }
})

$("#log").click(function (){
    let uname = $("#log_uname").val();
    let password =$("#log_password").val();

    if (uname!=""){
        console.log("123")
        $.post("/yuezhi/login/log.action",{"uname":uname},function (data){
            console.log(data.data);
            if (password==data.data&&data.code=="01"){
                $("#log_msg").text("登录成功")
                $.post("/yuezhi/user/person.action",{"uname":uname},function (){
                    window.location="/yuezhi/user/info.action";
                })
            }else{
                $("#log_msg").text("用户名或密码有误")
            }
        })
    }else{
        $("#log_msg").text("用户名不能为空")
    }

})